package com.fragments;

import java.util.ArrayList;

import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.Utility.MessageManager;
import com.Utility.Utility;
import com.Utility.WebServiceCommunicator.WebServiceFlag;
import com.activities.ContactAcitivity;
import com.activities.EditGroupActivity;
import com.activities.group.TabContactGroupActivity;
import com.adapters.GroupContactAdapter;
import com.adapters.db.ContentProviderDB;
import com.adapters.db.GroupTableAdapter;
import com.controls.ws.ContactWSControl;
import com.controls.ws.GroupWSControl;
import com.dialogs.LoadingDialog;
import com.events.WebServiceCommunicatorListener;
import com.objects.Group;
import com.senditcertified.mail.R;

public class GroupContactFragment extends BaseFragment implements
		OnClickListener, WebServiceCommunicatorListener {
	private ListView mLstGroup;
	private TextView mTvNodata;
	private View mVAddGroup, mVColGroupName;
	private EditText mEdtAddGroup;
	private GroupWSControl mWSGroup;
	private LoadingDialog mDialog;
	private GroupContactAdapter mAdapter;
	private GroupTableAdapter mTAGroup;
	private ContactAcitivity mContactActivity;
	private ContactWSControl mWSContact;

	@Override
	protected int initLayout() {
		return R.layout.fragment_group_contact;
	}

	@Override
	protected void initViews() {
		mTvNodata = (TextView) getView().findViewById(R.id.tv_nodata);
		mLstGroup = (ListView) getView().findViewById(R.id.list);
		mVAddGroup = getView().findViewById(R.id.ln_add_new_group);
		mEdtAddGroup = (EditText) getView().findViewById(R.id.edt_add_group);
		mVColGroupName = getView().findViewById(R.id.tv_col_group_name);

	}

	private void setAdapter(ArrayList<Group> lstItem) {
		if (lstItem != null && lstItem.size() > 0) {
			mAdapter = new GroupContactAdapter(getActivity(), lstItem) {
				@Override
				public void onViewEdit(int pos) {
					Intent i = new Intent(
							getActivity().getApplicationContext(),
							EditGroupActivity.class);
					i.putExtra(EditGroupActivity.KEY_GROUP_ITEM,
							mAdapter.getItem(pos));
					((TabContactGroupActivity) getActivity().getParent())
							.startChildActivity(
									EditGroupActivity.class.getSimpleName(), i);
				}
			};
			mTvNodata.setVisibility(View.GONE);
			mLstGroup.setVisibility(View.VISIBLE);
			mLstGroup.setAdapter(mAdapter);
		} else {
			mTvNodata.setVisibility(View.VISIBLE);
			mLstGroup.setVisibility(View.GONE);
			mAdapter = null;
		}

	}

	public void getData() {
		ArrayList<Group> lst = getGroupSortOrder();
		if (lst != null && lst.size() > 0) {
			setAdapter(lst);
		}
		mWSGroup.getGroups(Utility.getStringPreference(getActivity(),
				Utility.KEY_LOGIN_TOKEN, ""));
	}

	public ArrayList<Group> getGroupSortOrder() {
		String sortOrder = null;
		if (mContactActivity.isGroupNameAsc()) {
			sortOrder = ContentProviderDB.COL_TITLE + " ASC";
		} else {
			sortOrder = ContentProviderDB.COL_TITLE + " DESC";
		}
		return mTAGroup.getItems(sortOrder);
	}

	@Override
	protected void initComponents() {
		// TODO Auto-generated method stub]
		mTAGroup = new GroupTableAdapter(getActivity());
		mWSGroup = new GroupWSControl(getActivity(), this);
		mContactActivity = (ContactAcitivity) getActivity();
		getData();
	}

	@Override
	protected void addListener() {
		mVAddGroup.setOnClickListener(this);
		mVColGroupName.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		if (v == mVAddGroup) {
			String name = mEdtAddGroup.getText().toString().trim();
			if (name.length() > 0) {
				mWSGroup.addGroup(Utility.getStringPreference(getActivity(),
						Utility.KEY_LOGIN_TOKEN, ""), name);
			}
		} else if (v == mVColGroupName) {
			mContactActivity
					.setGroupNameAsc(!mContactActivity.isGroupNameAsc());
			setAdapter(getGroupSortOrder());
		}
	}

	@Override
	public void onConnectionOpen(WebServiceFlag flag) {
		if (flag == WebServiceFlag.GET_GROUP) {
			if (mAdapter != null && mAdapter.getCount() > 0) {
				return;
			}
		}
		if (mDialog != null) {
			mDialog.dismiss();
			mDialog = null;
		}
		mDialog = new LoadingDialog(getActivity().getParent());
		mDialog.setCancelable(false);
		mDialog.show();

	}

	@Override
	public void onConnectionError(WebServiceFlag flag, String error) {
		if (mDialog != null) {
			mDialog.dismiss();
			mDialog = null;
		}
		MessageManager.showToast(getActivity(), error);

	}

	@Override
	public void onConnectionDone(WebServiceFlag flag, String response) {
		if (mDialog != null) {
			mDialog.dismiss();
			mDialog = null;
		}

		if (flag == WebServiceFlag.GET_GROUP) {
			ArrayList<Group> lst = mWSGroup.parseGroups(response);
			mTAGroup.clear();
			for (int i = 0; i < lst.size(); i++) {
				try {
					mTAGroup.add(lst.get(i), i);
				} catch (Exception e) {
				}
			}
			setAdapter(getGroupSortOrder());
		} else if (flag == WebServiceFlag.ADD_GROUP) {
			Group item = mWSGroup.parseAddGroups(response);
			if (item != null && item.getTitle() != null
					&& item.getTitle().length() > 0) {
				MessageManager.showToast(getActivity(), item.getTitle());
			}
			mWSGroup.getGroups(Utility.getStringPreference(getActivity(),
					Utility.KEY_LOGIN_TOKEN, ""));
		}

	}

	@Override
	public void onDestroyView() {
		if (mWSGroup != null) {
			mWSGroup.cancel();
			mWSGroup = null;
		}
		if (mDialog != null) {
			mDialog.dismiss();
			mDialog = null;
		}

		super.onDestroyView();
	}

}
